#!/bin/bash

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
}

# 检查docker和docker-compose是否安装
check_dependencies() {
    if ! command -v docker &> /dev/null; then
        print_message "$RED" "错误: 未找到 docker 命令，请先安装 Docker"
        exit 1
    fi

    if ! command -v docker-compose &> /dev/null; then
        print_message "$RED" "错误: 未找到 docker-compose 命令，请先安装 Docker Compose"
        exit 1
    fi
}

# 创建必要的目录
create_directories() {
    if [ ! -d "data" ]; then
        print_message "$YELLOW" "创建 data 目录..."
        mkdir -p data
        chmod 777 data
    fi
}

# 主程序
print_message "$YELLOW" "开始安装..."

# 检查依赖
check_dependencies

# 创建目录
create_directories

# 停止现有服务
print_message "$YELLOW" "停止现有服务..."
docker-compose down --remove-orphans

# 构建并启动服务
print_message "$YELLOW" "构建并启动服务..."
docker-compose up --build -d

# 检查启动结果
if [ $? -eq 0 ]; then
    print_message "$GREEN" "服务安装成功！"
    print_message "$GREEN" "访问地址: http://localhost:5000"
else
    print_message "$RED" "服务启动失败，请检查日志"
    docker-compose logs
    exit 1
fi 